package com.atguigu.apiservice.controller;


import com.atguigu.apiservice.entity.Book;
import com.atguigu.apiservice.service.BookService;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.VO.BookQueryVo;
import com.atguigu.servicebase.VO.FrontVo;
import com.atguigu.servicebase.VO.OneBookVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-03-26
 */
@Api(description = "首页数据展示")
@RestController
@RequestMapping("/apiservice/book")
@CrossOrigin
public class BookController {

    @Autowired
    private BookService bookService;

    /**
     * 分页展示数据书籍数据
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation("分页展示数据书籍数据")
    @GetMapping("index/{page}/{limit}")
    public R  getIndexData(@PathVariable Long page,@PathVariable Long limit){
        Page<Book> pageParam = new Page<>(page,limit);
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("creation_time");
        bookService.page(pageParam,queryWrapper);
        List<Book> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("items", records);
    }

    /**
     * 前端最热书单展示
     * @return
     */
    @ApiOperation("前端最热书单展示")
    @GetMapping("hotindex")
    public R gethotindex(){
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("clickNum");
        wrapper.last("limit 4");
        List<Book> list = bookService.list(wrapper).stream().sorted(Comparator.comparing(Book::getClickNum)).collect(Collectors.toList());
        return R.ok().data("hotbooklist",list);
    }

    /**
     * 分页展示书籍数据带条件
     */
    @ApiOperation("分页展示书籍数据带条件")
    @PostMapping("indexVo/{current}/{limit}")
    public R getIndexDataVo(@PathVariable Long current,
                            @PathVariable Long limit,
                            @RequestBody BookQueryVo bookQueryVo){

        Page<Book> pageParam = new Page<>(current,limit);
        Map<String,Object> maps = bookService.pageListVo(pageParam,bookQueryVo);
        return R.ok().data(maps);
    }

    /**
     * 根据书籍Id获取书籍详情
     */
    @ApiOperation("根据书籍Id获取书籍详情")
    @GetMapping("getBookInfo/{id}")
    public R getBookInfo(@PathVariable String id){

        //获得课程详情信息
        FrontVo bookInfo = bookService.getBookInfo(id);
        return R.ok().data("frontVo",bookInfo);

    }

    /**
     * 查询所有一级分类
     */
    @ApiOperation("查询所有一级分类")
    @GetMapping("getAllBookLable")
    public R getAllBookLable(){
        List<OneBookVo> allbooklable = bookService.getAllBookLable();
        return R.ok().data("allbooklable",allbooklable);
    }


    @PostMapping("infoupload")
    public void infoupload(@RequestBody Book book){
        bookService.infoupload(book);
    }

}

